﻿using System;
using System.Data;
using System.Collections;

public partial class Simple : System.Web.UI.Page
{
    public Data.VarParams varParam = new Data.VarParams();
    public Data.Data currentData = new Data.Data();
    public Data.DataConsolidation dc = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        String errMsg = varParam.ReadFile(Request.PhysicalApplicationPath + "DemoData\\", this.Session.SessionID);
        if (errMsg == null)
        {
            dc = new Data.DataConsolidation(varParam);

            String projectFileName = "00 MissingValuesProblems.txt";
            Boolean error = ExecuteProjectFile( projectFileName );
            if (error == false)
            {
                Data.Page currentPage = currentData.GetSelectedPage();

                makeChart(currentPage);
            }
        }
    }
    Boolean ExecuteProjectFile( String projectFileName )
    {
        String fullProjectFileName = varParam.GetMyProjectsDir() + projectFileName;
        if ( System.IO.File.Exists(fullProjectFileName))
        {
            String [] projectItems = System.IO.File.ReadAllLines( fullProjectFileName );

            foreach( String projectItem in projectItems )
            {
                Boolean initialLoad = true;
                Boolean success = dc.RunScript(projectItem, currentData, initialLoad);
                if (success == false)
                    return success;
            }
            return false;
        }
        return false;
    }


    private void makeChart( Data.Page currentPage )
    {
        if (currentPage == null)
            return;
        // Enable X axis labels automatic fitting
        chart1.ChartAreas[0].AxisX.LabelsAutoFit = true;

        // Set X axis automatic fitting style
        chart1.ChartAreas[0].AxisX.LabelsAutoFitStyle =
            Dundas.Charting.WebControl.LabelsAutoFitStyle.DecreaseFont
            | Dundas.Charting.WebControl.LabelsAutoFitStyle.IncreaseFont
            | Dundas.Charting.WebControl.LabelsAutoFitStyle.WordWrap;

        // Set interval of X axis to every row
        chart1.ChartAreas["Default"].AxisX.Interval = 1;

        chart1.Titles.Add(currentPage.GetPageCaption());
        chart1.Titles.Add(currentPage.GetPageDescription());

        if (currentPage.GetNumericColumnCount() < 2)
            chart1.Legends[0].Enabled = false;

        chart1.Series.Clear();
        int rowCount = currentPage.GetRowCount();
        for (int columnIndex = 0, columnCount = currentPage.GetNumericColumnCount();
                columnIndex < columnCount ; columnIndex++ )
        {
            String seriesName = currentPage.GetNumericColumnName(columnIndex);
            Dundas.Charting.WebControl.Series thisSeries
                = chart1.Series.Add(seriesName, rowCount);

            for (int rowIndex = 0;
                    rowIndex < rowCount; rowIndex++)
            {
                String rowCaption = currentPage.GetRowLabelValue(rowIndex);

                //String alertLevel = currentPage.GetAlertLevel(rowIndex, columnIndex);
                //if (alertLevel.Equals("-1") == false)
                //    rowCaption += ", alert " + alertLevel;

                Double rowValue = currentPage.GetValue(rowIndex, columnIndex);
                if (rowValue == Data.Data.BadNum())
                    rowValue = 0;

                thisSeries.Points.AddXY(rowCaption, rowValue );

                thisSeries.Points[rowIndex].ToolTip = rowCaption + " " + seriesName;
            }
        }

    }

}



